package com.op.aspect;

import com.op.core.service.impl.MailService;
import com.op.entity.User;
import com.op.response.ResponseResult;
import com.op.service.UserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * ClassName： MailAspect
 * Date： 2021/12/13 16:58
 *
 * @author 孤焰
 * @version 1.0
 **/

@Component
@Aspect
public class MailAspect {

    @Resource
    UserService userService;
    @Resource
    MailService mailService;

    @Around(value = "@annotation(com.op.aspect.MailNotice)")
    public ResponseResult<Boolean> mailNoticeAround(ProceedingJoinPoint joinPoint) throws Throwable {
        ResponseResult<Boolean> functionResult = (ResponseResult<Boolean>) joinPoint.proceed();
        // 通过userId获取到用户邮箱和电话，然后发送邮件通知账号审批情况
        User userParam = (User) joinPoint.getArgs()[0];
        User userInfo = userService.getById(userParam.getId());
        mailService.sendTemplate(userInfo.getEmail(), userInfo.getPhone());
        return functionResult;
    }

    @Around(value = "@annotation(com.op.aspect.MailTempAccount)")
    public ResponseResult<Boolean> MailTempAccountAround(ProceedingJoinPoint joinPoint) throws Throwable {
        ResponseResult<Boolean> functionResult = (ResponseResult<Boolean>) joinPoint.proceed();
        // 通过userId获取到用户邮箱和电话，然后发送邮件通知账号审批情况
        User userInfo = (User) joinPoint.getArgs()[0];
        mailService.sendTemplate2(userInfo.getEmail(), userInfo.getPhone(), userInfo.getPassword());
        return functionResult;
    }
}
